package edu.jiefengli.course_selection_system.controller;

import edu.jiefengli.course_selection_system.pojo.entity.Course;
import edu.jiefengli.course_selection_system.pojo.entity.CourseRecord;
import edu.jiefengli.course_selection_system.pojo.entity.Result;
import edu.jiefengli.course_selection_system.pojo.entity.User;
import edu.jiefengli.course_selection_system.pojo.vo.SelectRecord;
import edu.jiefengli.course_selection_system.service.CourseRecordService;
import edu.jiefengli.course_selection_system.service.CourseService;
import edu.jiefengli.course_selection_system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;
    @Autowired
    private CourseRecordService courseRecordService;
    @Autowired
    private CourseService courseService;

    @PostMapping("/user")
    public Result addUser(@RequestBody User newUser) {
        if (newUser.getPassword() == null ||
                newUser.getPassword().trim() == "") {
            return Result.error("密码不能为空");
        }

        if (newUser.getName() == null || newUser.getName().trim() == "") {
            return Result.error("用户名不能为空");
        }

        if (newUser.getRole() == null) {
            return Result.error("角色不能为空");
        }
        newUser.setPassword(DigestUtils.md5DigestAsHex(newUser.getPassword().getBytes()));
        System.out.println("添加用户: " + newUser);
        userService.addUser(newUser);
        return Result.success();
    }

    @GetMapping("/user")
    public Result<List<User>> getAllUsers() {
        List<User> allUsers = userService.getAllUsers();
        for (User user : allUsers) {
            user.setPassword("***");
        }
        return Result.success(allUsers);
    }

    @PostMapping("course")
    public Result addCourse(@RequestBody Course course) {
        System.out.println("添加课程：" + course);
        courseService.addCourse(course);
        return Result.success();
    }

    @GetMapping("/courses")
    public Result<List<Course>> getAllCourses() {
        return Result.success(courseService.getAllCourses());
    }

    @GetMapping("/record")
    public Result<List<SelectRecord>> getAllCourseRecords() {
        System.out.println("获取全部选课记录");
        List<CourseRecord> allRecords = courseRecordService.getAllRecords();
        List<SelectRecord> selectRecords = new ArrayList<>();
        for (CourseRecord record : allRecords) {
            SelectRecord sr = new SelectRecord();
            Course course = courseService.getCourseById(record.getCourseId());
            User student = userService.getUserById(record.getStudentId());
            User teacher = userService.getUserById(course.getTeacherId());
            sr.setCourseRecord(record);
            sr.setCourse(course);
            sr.setStudent(student);
            sr.setTeacher(teacher);
            selectRecords.add(sr);
        }
        return Result.success(selectRecords);
    }
}
